package com.example.CarFee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.example.trainfee.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class RecordAcitvity extends Activity {
	private ListView listView;
	private SimpleAdapter adapter;
	private List<Map<String, String>> items;
	private ImageView backBtn;
	private String[] info;
	private Button tuidingBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_record);
		listView = (ListView) findViewById(R.id.listView);
		backBtn = (ImageView) findViewById(R.id.backBtn);
		items = new ArrayList<Map<String, String>>();
		tuidingBtn = (Button) findViewById(R.id.tuiding);
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				info = Service.checkPersonalInfo();
				if (!info[3].equals("")) {
					handler.sendEmptyMessage(0x120);
				} else {
					handler.sendEmptyMessage(0x121);
				}
			}
		});
		thread.start();

		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		tuidingBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						if (Service.CancelTicket()) {
							handler.sendEmptyMessage(0x122);
						} else {
							handler.sendEmptyMessage(0x123);
						}
					}
				});
				thread.start();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				tuidingBtn.setVisibility(View.VISIBLE);
			}
		});
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x120) {
				for (int i = 0; i < 1; i++) {
					HashMap<String, String> item = new HashMap<String, String>();
					item.put("name", info[0]);
					item.put("place", info[3]);
					item.put("date", info[5]);
					item.put("time", info[6]);
					items.add(item);
				}
				adapter = new SimpleAdapter(RecordAcitvity.this, items,
						R.layout.record_listview_item, new String[] { "name",
								"place", "date", "time" }, new int[] {
								R.id.name, R.id.place, R.id.date, R.id.time });
				listView.setAdapter(adapter);
			}
			if (msg.what == 0x121) {
				Toast.makeText(RecordAcitvity.this, "暂无订票情况",
						Toast.LENGTH_SHORT).show();
			}
			if (msg.what == 0x122) {
				Toast.makeText(RecordAcitvity.this, "退订成功", Toast.LENGTH_SHORT)
						.show();
				finish();
			}
			if (msg.what == 0x123) {
				Toast.makeText(RecordAcitvity.this, "退订失败", Toast.LENGTH_SHORT)
						.show();
			}
		}
	};
}
